package com.weaver.openapi.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.openapi.util.*;
import cn.hutool.http.*;
import cn.hutool.json.*;
import com.weaver.openapi.pojo.dept.params.*;
import com.weaver.openapi.pojo.dept.res.*;
import com.alibaba.fastjson.*;
import cn.hutool.core.collection.*;
import java.util.*;
import com.weaver.openapi.pojo.*;

public class DeptService
{
    public static DeptList listDeptV2(final DeptListParam deptListParam, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", deptListParam.getAccessToken());
        paramMap.put("depid", deptListParam.getDeptId());
        paramMap.put("status", deptListParam.getStatus());
        paramMap.put("isDelete", deptListParam.getIsDelete());
        final String resJson = ((HttpRequest)HttpRequest.get(host + "/department/v2/list").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (DeptList)JSONUtil.toBean(resJson, (Class)DeptList.class);
    }
    
    public static NormalRes<DeptCreate> createDeptV2(final DeptParamVo createDeptParam, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", createDeptParam.getAccessToken());
        paramMap.put("parentId", createDeptParam.getParentId());
        paramMap.put("name", createDeptParam.getName());
        paramMap.put("description", createDeptParam.getDescription());
        paramMap.put("disporder", createDeptParam.getDisporder());
        paramMap.put("code", createDeptParam.getCode());
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/department/v2/create").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return transferResult(resJson);
    }
    
    public static MessageResult updateDeptV2(final DeptParamVo createDeptParam, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", createDeptParam.getAccessToken());
        paramMap.put("parentId", createDeptParam.getParentId());
        paramMap.put("name", createDeptParam.getName());
        paramMap.put("description", createDeptParam.getDescription());
        paramMap.put("disporder", createDeptParam.getDisporder());
        paramMap.put("code", createDeptParam.getCode());
        paramMap.put("depid", createDeptParam.getDeptId());
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/department/v2/update").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (MessageResult) JSONObject.parseObject(resJson, (Class)MessageResult.class);
    }
    
    public static MessageResult delDeptV2(final String accessToken, final Long deptId, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", accessToken);
        paramMap.put("depid", deptId);
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/department/v2/update").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (MessageResult)JSONObject.parseObject(resJson, (Class)MessageResult.class);
    }
    
    public static DeptVo getDeptV2(final String accessToken, final Long deptId, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", accessToken);
        paramMap.put("depid", deptId);
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/department/v2/get").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (DeptVo)JSONObject.parseObject(resJson, (Class)DeptVo.class);
    }
    
    public static DeptList findUpdateDepartmentV2(final String accessToken, final String beginDate, final String endDate, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", accessToken);
        paramMap.put("beginDate", beginDate);
        paramMap.put("endDate", endDate);
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/department/v2/findUpdateDepartment").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (DeptList)JSONObject.parseObject(resJson, (Class)DeptList.class);
    }
    
    public static NormalRes<List<DeptDisable>> disableDepartmentV2(final String accessToken, final List<Long> ids, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/json");
        final JSONArray bodyArr = new JSONArray();
        final JSONObject body = new JSONObject();
        if (!CollectionUtil.isEmpty((Collection)ids)) {
            JSONArray jsonArray = new JSONArray();
            ids.forEach(id -> {
                JSONObject item = new JSONObject();
                item.put("id", (Object)id);
                jsonArray.add((Object)item);
                return;
            });
        }
        body.put("datas", (Object)bodyArr);
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/api/hrm/restful/disableDepartment?access_token=" + accessToken).body(body.toJSONString()).headerMap((Map)headers, true)).execute().body();
        return transferListResult(resJson);
    }
    
    public static NormalRes<List<DeptDisable>> enableDepartmentV2(final String accessToken, final List<Long> ids, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/json");
        final JSONArray bodyArr = new JSONArray();
        final JSONObject body = new JSONObject();
        if (!CollectionUtil.isEmpty((Collection)ids)) {
            JSONArray jsonArray = new JSONArray();
            ids.forEach(id -> {
                JSONObject item = new JSONObject();
                item.put("id", (Object)id);
                jsonArray.add((Object)item);
                return;
            });
        }
        body.put("datas", (Object)bodyArr);
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/api/hrm/restful/enableDepartment?access_token=" + accessToken).body(body.toJSONString()).headerMap((Map)headers, true)).execute().body();
        return transferListResult(resJson);
    }
    
    public static NormalRes<DeptCreate> transferResult(final String resJson) {
        final JSONObject jsonObject = JSONObject.parseObject(resJson);
        final DeptCreate data = (DeptCreate)jsonObject.getObject("data", (Class)DeptCreate.class);
        final NormalResult message = (NormalResult)jsonObject.getObject("message", (Class)NormalResult.class);
        final NormalRes<DeptCreate> normalRes = new NormalRes<DeptCreate>();
        normalRes.setData(data);
        normalRes.setMessage(message);
        return normalRes;
    }
    
    public static NormalRes<List<DeptDisable>> transferListResult(final String resJson) {
        final JSONObject jsonObject = JSONObject.parseObject(resJson);
        final List<DeptDisable> data = (List<DeptDisable>)jsonObject.getJSONArray("data").toJavaList((Class)DeptDisable.class);
        final NormalResult message = (NormalResult)jsonObject.getObject("message", (Class)NormalResult.class);
        final NormalRes<List<DeptDisable>> normalRes = new NormalRes<List<DeptDisable>>();
        normalRes.setData(data);
        normalRes.setMessage(message);
        return normalRes;
    }
}
